HAL PKA initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 PKA configuration panel.

  • Configure the SW layer HAL.

  • Configure the NVIC if the application requires the PKA to be used in IT mode.

  • Generate the code.

HAL PKA configuration panel

Create a new project with the STM32CubeMX2, then start the initialization.

Project configuration panel

NVIC configuration

Configure the NVIC in case of an interrupt.

PKA NVIC configuration

Generated PKA initialization and de-initialization sequences

The PKA initialization sequence is generated in the mx_pka_init function in mx_pka.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : PKA initialization and de-initialization sequences

Topic

Code Snippet HAL1

Code Snippet HAL2

Handle declaration

PKA_HandleTypeDef hpka;
hal_pka_handle_t hPKA;

Initialization and configuration sequence

static void MX_PKA_Init(void)
{
  hpka.Instance = PKA;
  if (HAL_PKA_Init(&hpka) != HAL_OK)
  {
    Error_Handler();
  }
}

void HAL_PKA_MspInit(PKA_HandleTypeDef* hpka)
{
  if(hpka->Instance==PKA)
  {
    /* Peripheral clock enable */
    __HAL_RCC_PKA_CLK_ENABLE();
    /* PKA interrupt Init */
    HAL_NVIC_SetPriority(PKA_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(PKA_IRQn);
   }
 }
hal_pka_handle_t *mx_pka_init(void)
{
  if (HAL_PKA_Init(&hPKA, HAL_PKA1) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_RNG_EnableClock();

  HAL_RCC_PKA_EnableClock();

  HAL_RCC_RNG_EnableClock();

  if (HAL_RCC_CK48_SetKernelClkSource(HAL_RCC_CK48_CLK_SRC_HSIDIV3) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for RNG */
  HAL_CORTEX_NVIC_SetPriority(PKA_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(PKA_IRQn);
  return &hPKA;
}

Deinitialization sequence

void MX_PKA_DeInit(void)
{
  if (HAL_PKA_DeInit(&hpka) != HAL_OK)
  {
    Error_Handler();
  }
}
void HAL_PKA_MspDeInit(PKA_HandleTypeDef* hpka)
{
  if(hpka->Instance==PKA)
  {
    /* Peripheral clock disable */
    __HAL_RCC_PKA_CLK_DISABLE();

    /* PKA interrupt DeInit */
    HAL_NVIC_DisableIRQ(PKA_IRQn);
   }
 }
void mx_pka_deinit(void)
{
  (void)HAL_PKA_DeInit(&hPKA);
}

IRQ handle function

void PKA_IRQHandler(void)
{
  HAL_PKA_IRQHandler(&hpka);
}
void PKA_IRQHandler(void)
{
  HAL_PKA_IRQHandler(&hPKA);
}